package com.zhkj.txg.utils

import android.content.Context
import android.graphics.BitmapFactory
import com.zhkj.lib.config.WX_APP_ID
import com.zhkj.lib.utils.ToastUtil
import com.zhkj.txg.R
import com.tencent.mm.opensdk.modelmsg.*
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import io.reactivex.Observable
import io.reactivex.schedulers.Schedulers
import top.zibin.luban.Luban

object ShareUtils {
    /**
     * 发圈素材分享到朋友圈
     */
    fun share2Circle(context: Context, content: String, imageFilePath: String) {
        val msgApi = WXAPIFactory.createWXAPI(context, null)
        if (!msgApi.isWXAppInstalled) {
            ToastUtil.show(context.getString(R.string.wechat_not_install))
        } else {
            val wxMessage: WXMediaMessage = if (imageFilePath.isNotEmpty()) WXMediaMessage(WXImageObject().apply {
                setImagePath(imageFilePath)
            }) else WXMediaMessage(WXTextObject(content))
            msgApi.registerApp(WX_APP_ID)
            msgApi.sendReq(SendMessageToWX.Req().apply {
                message = wxMessage
                scene = SendMessageToWX.Req.WXSceneTimeline
            })
        }
    }

    /**
     * 分享图片给好友
     */
    fun shareImage2Friends(context: Context, imageFilePath: String, share2Friends: Boolean = true) {
        val msgApi = WXAPIFactory.createWXAPI(context, null)
        if (!msgApi.isWXAppInstalled) {
            ToastUtil.show(context.getString(R.string.wechat_not_install))
        } else {
            val wxMessage = WXMediaMessage(WXImageObject().apply { setImagePath(imageFilePath) })
            msgApi.registerApp(WX_APP_ID)
            msgApi.sendReq(SendMessageToWX.Req().apply {
                message = wxMessage
                scene = if (share2Friends) SendMessageToWX.Req.WXSceneSession else SendMessageToWX.Req.WXSceneTimeline
            })
        }
    }

    /**
     * 分享链接给好友
     */
    fun shareUrl2Friends(
        context: Context,
        title: String,
        content: String,
        imageFileUrl: String?,
        url: String,
        shareFriends: Boolean = false
    ) {
        val msgApi = WXAPIFactory.createWXAPI(context, null)
        if (!msgApi.isWXAppInstalled) ToastUtil.show(context.getString(R.string.wechat_not_install))
        else Observable.create<WXMediaMessage> { emitter ->
            emitter.onNext(WXMediaMessage(WXWebpageObject(url)).apply {
                this.title = title
                this.description = content
                val bitmap = if (imageFileUrl.isNullOrEmpty()) BitmapFactory.decodeResource(
                    context.resources,
                    R.drawable.ic_logo
                ) else {
                    val cacheFile = ImageUtils.getCacheImage(context, imageFileUrl)
                    val file = Luban.with(context).setTargetDir(context.cacheDir.path)
                        .setFocusAlpha(false).load(cacheFile).get()[0]
                    val realFile = Luban.with(context).setTargetDir(context.cacheDir.path)
                        .setFocusAlpha(false).load(file).get()[0]
                    val bitmap = BitmapFactory.decodeFile(realFile.path, BitmapFactory.Options().apply {
                        inSampleSize = 2
                    })
                    file.delete()
                    bitmap
                }
                setThumbImage(bitmap)
                bitmap.recycle()
            })
            emitter.onComplete()
        }.subscribeOn(Schedulers.io()).subscribe({
            msgApi.registerApp(WX_APP_ID)
            msgApi.sendReq(SendMessageToWX.Req().apply {
                message = it
                scene = if (shareFriends) SendMessageToWX.Req.WXSceneSession else SendMessageToWX.Req.WXSceneTimeline
            })
        }, { it.printStackTrace() })
    }

    fun jump2WeChat(context: Context) {
        val msgApi = WXAPIFactory.createWXAPI(context, null)
        if (!msgApi.isWXAppInstalled) {
            ToastUtil.show(context.getString(R.string.wechat_not_install))
        } else {
            msgApi.openWXApp()
        }
    }
}